package com.icode.web.controller.User;

import com.icode.core.dto.user.ProvinceDTO;
import com.icode.core.dto.user.UsersInfoDTO;
import com.icode.core.model.user.*;
import com.icode.core.shared.DataToJson;
import com.icode.core.shared.SecurityUtil;
import com.icode.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-5
 * Time: 下午4:00
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/front/user_info")
public class UsersInfoController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "form", method = RequestMethod.GET)
    private ModelAndView showForm() {
        User currentUser = SecurityUtil.currentUser();
        UsersInfoDTO usersInfoDTO = userService.initUserInfoDTO(currentUser);
//        List<Province> provinces = userService.findProvince();
//        usersInfoDTO.toProvinceDTO(provinces);
        return new ModelAndView("front/user/userInfo", "usersInfoDTO", usersInfoDTO);
    }


    @RequestMapping(value = "save", method = RequestMethod.POST)
    private String saveOrUpdateUsersInfo(UsersInfoDTO usersInfoDTO, BindingResult bindingResult) {
//        if (bindingResult.hasErrors()) {
//            return "front/user/userInfo";
//        }
        User currentUser = SecurityUtil.currentUser();
        UserInfo userInfo = userService.findUserInfoByUser(currentUser);
        userService.saveOrUpdateUsersInfo(usersInfoDTO, userInfo);
        return "redirect:/front/user_info/form";
    }


    @RequestMapping(value = "findProvince", method = RequestMethod.POST)
    private void findProvince(HttpServletResponse response) {
        List<Province> provinces = userService.findProvince();
        DataToJson.ProvinceToJson(provinces, response);
    }

    @RequestMapping(value = "findCity", method = RequestMethod.POST)
    private void findCity(String provinceId, HttpServletResponse response) {
        List<City> cities = userService.findCityById(provinceId);
        DataToJson.CityToJson(cities, response);
    }

    @RequestMapping(value = "findDistrict", method = RequestMethod.POST)
    private void findDistrict(String cityId, HttpServletResponse response) {
        List<District> districts = userService.findDistrictById(cityId);
        DataToJson.DistrictToJson(districts, response);
    }

    @RequestMapping(value = "accountInfo", method = RequestMethod.GET)
    private String accountInfo() {
        return "front/user/AccountInfo";
    }

    /*
    for navigation get the user info of current user
     */
    @RequestMapping(value = "userinfo")
    @ResponseBody
    public Object getUserInfo() {
        User user = SecurityUtil.currentUser();
        Map<String, String> model = new HashMap<>();
        if (user == null) {
            return null;
        }
        String username = user.getUsername();
        UserInfo userInfo = userService.findUserInfoByUsername(username);
        if (userInfo == null || userInfo.usersInfoImage() == null) {
            model.put("username", username);
            model.put("imageGuid", null);
            return model;
        }
        String imageGuid = userInfo.usersInfoImage().guid();
        model.put("username", userInfo.username());
        model.put("imageGuid", imageGuid);
        return model;
    }
}
